// todo 渲染 我的文章
function renderList() {
  axios.get('http://124.223.14.236:8060/user/myArticle?page=1&limit=10')
    .then(({
      data: {
        data
      }
    }) => {

      const {
        count
      } = data
      console.log(data)
      const dataArr = data.data.map(function (ele) {
        let {
          click,
          id,
          tags,
          title,
          pic,
          lovenum,
          status
        } = ele
        console.log(id)
        const picIng = `http://124.223.14.236:8060/${pic}`
        if (status !== 1) {
          return `
          <li>
           <a href="JavaScript:;">
            <span class="thumbnail">
              <img class="thumb"src="${pic===''?'./images/default.png':picIng}"/>
               </span>
              <span class="text">${title}<span><!----><span>【已发布】</span></span></span>
              <span class="text-muted post-views"> <i class="fa fa-eye"></i> 已阅读(${click===null?"0":click})</span>
              <span class="text-muted post-views"> <i class="fa fa-heart"></i> 被收藏(${lovenum===null?"0":lovenum})</span>
            <span class="text-muted post-views"> <i class="fa thumbs-up"></i> ${tags}</span>
              </a>
          </li>
          `
        } else {
          // status===1
          return `
          <li>
            <a href="javascript:;">
              <span class="thumbnail">
                <img class="thumb" src="${pic===''?'./images/default.png':picIng}" />
              </span>
              <span class="text">${title}<span><!----><span>【未发布】</span></span></span>
              <div class="action-btn">
                <button class="btn btn-primary btn-xs" data-id='${id}'><i class="fa fa-edit"></i>&nbsp;修改</button>
                <button class="btn btn-success btn-xs" data-id='${id}'><i class="fa fa-send"></i>&nbsp;发布</button>
                <button class="btn btn-danger btn-xs"  data-id='${id}'><i class="fa fa-trash"></i>&nbsp;删除</button>
              </div>
            </a>
          </li>
          `
        }
      }).join('')
      $('.items-01').html(dataArr)
      renderfen(count)
    })
}
renderList()
// todo  渲染分页功能函数
let pagenum = 1

function renderfen(count) {
  layui.use(function () {
    // 开启 HASH
    laypage.render({
      elem: 'aa',
      count: count,
      theme: '#1E9FFF',
      curr: pagenum, // 初始获取 hash 值为 curr 的当前页
      layout: ['prev', 'page', 'next', 'count', 'refresh'],
      jump: function (obj, first) {
        //  得到当页值
        pagenum = obj.curr
        // 判断下一页 下一页是否显示
        if (pagenum === 1) {
          document.querySelector('.layui-laypage-prev').style.display = 'none'
        } else if (document.querySelector('.layui-disabled')) {
          document.querySelector('.layui-disabled').style.display = 'none'
        }
        //首次不执行
        if (!first) {
          //do somethingre
          renderList(obj.curr)
        }
      }
    });
  });
}

// todo 未发布 ===> 删除功能
$('body').on('click', '.btn-danger', async function () {
  const id = $(this).data('id')
  console.log(id)
  const {
    status
  } = await axios.get(`/user/delArticle?id=${id}`)
  // 判断是否删除成功
  console.log(status)
  if (status !== 200) {
    return layer.msg('删除失败')
  }
  layer.msg('删除成功', {
    icon: 1,
    time: 1000
  }, () => {
    renderList()
  })
})

// todo 未发布 ===> 发布功能
$('body').on('click', '.btn-success', async function () {
  const id = $(this).data('id')
  const {
    data: {
      data
    }
  } = await axios.get(`http://124.223.14.236:8060/index/show?id=${id}`)
  const {
    info
  } = data
  info.status = 2
  console.log(info)
  const {
    status
  } = await axios.post('http://124.223.14.236:8060/user/editArticle', info)
  console.log(status)
  // 判断是否发布成功
  if (status !== 200) {
    return layer.msg('发布失败')
  }
  layer.msg('发布成功', {
    icon: 1,
    time: 1000
  }, () => {
    renderList()
  })
})

// todo 未发布 ===> 编辑功能
$('body').on('click', '.btn-primary', async function () {
  const id = $(this).data('id')
  const {
    data: {
      data
    }
  } = await axios.get(`http://124.223.14.236:8060/index/show?id=${id}`)
  const {
    info
  } = data
  console.log(info)
  localStorage.setItem('info',JSON.stringify(info))
  layer.msg('进入编辑', {
    icon: 1,
    time: 1000
  }, () => {
    location.href='../public/user-add -edit.html'
  })
})